package com.example.administrator.yuejian_2.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.ContestCollectListAdapter;
import com.example.administrator.yuejian_2.bean.ContestCollectBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.UserDetailService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContestCollectListFragment extends Fragment {
    private RecyclerView recyclerView;
    private ContestCollectListAdapter adapter;
    private int uid;

    public ContestCollectListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_contest_collect_list, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);
        recyclerView = view.findViewById(R.id.contest_collect_recyclerView);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserDetailService service = retrofit.create(UserDetailService.class);
        Call<List<ContestCollectBean>> call = service.contestcollectlist(uid);
        call.enqueue(new Callback<List<ContestCollectBean>>() {
            @Override
            public void onResponse(Call<List<ContestCollectBean>> call, Response<List<ContestCollectBean>> response) {
                if (getActivity() != null)
                    adapter = new ContestCollectListAdapter(getActivity(),response.body());//adapter显示一条数据
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());//定义布局
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//定义布局
                recyclerView.setLayoutManager(layoutManager);//
                recyclerView.setAdapter(adapter);//调用适配器
                recyclerView.setItemAnimator(new DefaultItemAnimator());//调用item
            }

            @Override
            public void onFailure(Call<List<ContestCollectBean>> call, Throwable t) {

            }
        });




    }


}